package com.hundun.vision.biz.core;

import org.ta4j.core.*;
import org.ta4j.core.indicators.RSIIndicator;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.helpers.VolumeIndicator;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.rules.BooleanRule;
import org.ta4j.core.rules.OverIndicatorRule;
import org.ta4j.core.rules.UnderIndicatorRule;

import java.math.BigDecimal;

/**
 * @author ：RuoChen
 * @date ：14:10 2024/2/27
 * @description：自定义策略
 */
public class CustomStrategy {
    private static final String name = "自定义策略";

    public static Rule stopLossRule(TradingRecord tradingRecord, ClosePriceIndicator closePrice, BigDecimal stopLossPercentage) {
        Position currentPosition = tradingRecord.getCurrentPosition();
        if (currentPosition.isOpened()) {
            // 获取当前未平仓交易的入场价格
            double entryPrice = currentPosition.getEntry().getNetPrice().doubleValue();
            // 计算止损价格
            double stopLossPrice = currentPosition.getEntry().isBuy()
                    ? entryPrice * (1 - stopLossPercentage.doubleValue() / 100)
                    : entryPrice * (1 + stopLossPercentage.doubleValue() / 100);
            // 创建止损规则
            return currentPosition.getEntry().isBuy()
                    ? new UnderIndicatorRule(closePrice, DecimalNum.valueOf(stopLossPrice))
                    : new OverIndicatorRule(closePrice, DecimalNum.valueOf(stopLossPrice));
        }
        // 如果没有未平仓的 Position，则返回永不触发的规则
        return new BooleanRule(false);
    }
}
